package cn.ll.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExportMgr{
	
	

	private Object[] head;

	public WritableWorkbook getExcel(List list,OutputStream os) throws BiffException, IOException, RowsExceededException, WriteException{

		//OutputStream os = new FileOutputStream("E:/tt.xls");
	    WritableWorkbook wwb = Workbook.createWorkbook(os);
	    WritableSheet ws = wwb.createSheet("Sheet 1", 0);
	    this.getHead(ws,(Map)list.get(0));
	    for(int i = 0 ; i < list.size() ; i++){
	    	Map mp = (Map)list.get(i);
	    	this.tran1Row(ws,mp,i+1);
	    }
	    
	    //wwb.write();
	   // wwb.close();
		return wwb;
	}
	
	private void getHead(WritableSheet ws,Map mp) throws RowsExceededException, WriteException {
		
		Object [] keys = mp.keySet().toArray();
		this.head = keys;
		
		for(int i = 0 ; i < head.length ; i++){
			Object o = head[i];
		    Label labelC = new Label(i,0,o==null?"":o.toString());
		    ws.addCell(labelC);
		}
		
	}

	private void tran1Row(WritableSheet ws, Map mp,int rowNum) throws RowsExceededException, WriteException {
		
		for(int i = 0 ; i < head.length ; i++){
			Object o = mp.get((String)head[i]);
		    Label labelC = new Label(i,rowNum,o==null?"":o.toString());
		    ws.addCell(labelC);
		}
		
	}
	
	
	
}
